import 'package:flutter/material.dart';
import 'package:fluwx/fluwx.dart';

import 'package:url_launcher/url_launcher.dart';

class ToMpWx extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ToMpState();
  }
}

class ToMpState extends State<ToMpWx> {
  // String _result = '..';

  @override
  void initState() {
    super.initState();
    weChatResponseEventHandler.listen((res) {
      if (res is WeChatLaunchMiniProgramResponse) {
        if (mounted) {
          // setState(() {
          //   _result = "isSuccessful:${res.isSuccessful}";
          // });
          print(res);
        }
      }
    });
  }

  Widget build(BuildContext context) {
    _launchURL() async {
      const url = 'weixin://dl/business/?t=CzKprbBcBEd';
      if (await canLaunch(url)) {
        // 判断当前手机是否安装某app. 能否正常跳转
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    }

    return Center(
      child: Flex(
        direction: Axis.vertical,
        children: [
          TextButton(
            child: Text('跳转小程序'),
            onPressed: () {
              launchWeChatMiniProgram(
                username: 'gh_b342a34f1d40',
                path: '/pages/me/index',
              );
            },
          ),
          TextButton(
            child: Text('URL Scheme打开小程序'),
            onPressed: _launchURL,
          )
        ],
      ),
    );
  }
}
